<?php
	class Product_hot extends MY_Controller{	
		function Product_hot() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();
			$_SESSION['redirect'] = $this->uri->uri_string();
		}

		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Sản phẩm trong mục hot";
			$data['table'] = 'product';
			$data['controller'] = 'product';
			$data['content_admin'] = 'back_end/product_hot/index';
			$data['sort'] = 'ASC';
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			//lay cac cac comment va phan trang
			$this->db->select('product.name, product.image, product_hot.id');
			$this->db->from('product_hot');
			$this->db->join('product', 'product.id = product_hot.product_id');		
			$data['product_all'] = $this->db->get()->result_array();
			
			$this->load->view('back_end/template_backend',$data);
		} 
		
		function search(){
			$data['vl_sort'] = "DESC";	
			$data['sort'] = 'ASC';
			$data['table'] = 'product';
			$check_search = $this->input->POST('formSearch');
			$per_page = 6;
			$data['title'] = "Tìm kiếm - Sản phẩm";
			$data['controller'] = 'product';
			$data['list_categoryProduct'] = $this->db->get('category_product')->result_array();
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/product/index';

			if ($this->input->POST('category_product_id')) {
				$where = array(
					'category_product_id' => $this->input->POST('category_product_id')
				);	
			} else {
				$where = array();
			}
			
			if(!empty($check_search)){
				$_SESSION['like'] = array('name' =>$this->input->POST('nameSearch'));																												
			}
			
			if(empty($_SESSION['order_sort_product'])){
				$_SESSION['order_sort_product'] = array();	
			}
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			//lay ra noi xuat xu
			$data['origin'] = $this->db->get('origin')->result_array(); 
			//chọn thương hiệu 
			$data['brand'] = $this->db->get('brand')->result_array();
			//chọn nhà sản xuất
			$data['suppliers'] = $this->db->get('suppliers')->result_array();
			//lay ra danh muc san pham
			$data['category_product'] = $this->mod_lib->findOrderList('category_product');
			
			//join 

			$join['category_product'] =  'product.category_product_id = category_product.id';	

			if ($this->input->POST('brand_id')){
				$join['brand']            =  'product.brand_id = brand.id';	
			}
			if ($this->input->POST('origin_id')){
				$join['origin']           =  'product.origin_id = origin.id';
			}
			if ($this->input->POST('suppliers_id')){
				$join['suppliers']        =  'product.suppliers_id = suppliers.id';
			}
			
			//lay ra anh san pham
			$this->db->where('status',1);
			$data['image_prd']= $this->db->get('product_images')->result_array();
			
			$data_search            =  $this->mod_lib->join('product','product.*,category_product.name AS category_product_name',$where ,$join,$per_page,'',$_SESSION['order_sort_product']);																
			$data['query_prd']      =  $this->mod_lib->join('product','product.*,category_product.name AS category_product_name',$where ,$join,$per_page,'',$_SESSION['order_sort_product']);																													
			$config['base_url']     =  site_url('back_end/product/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function add(){			
			$data['title']		   = "Thêm sản phẩm vào mục sản phẩm hot";
			$data['content_admin'] = 'back_end/product_hot/add';
			$data['list_menu'] = $this->mod_lib->findOrderListCategory('category_product');
			$_SESSION['image_name'] = ' ';
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			// lay cac cac comment va phan trang
			$per_page = 24;
			$uri = 4;
			$data['product_all'] = $this->mod_lib->load_all('product','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/product_hot/add/');
			$config['total_rows']   =  $this->db->count_all('product');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);
			
			$this->load->view('back_end/template_backend',$data);
		}
		
		function add_hot() {
			$check_prd_all = $this->input->POST('check_prd_all');
			$this->db->select('product_id');
			$prd_hot = $this->db->get('product_hot')->result_array();
			
			if (!empty($check_prd_all)) {
				foreach ($prd_hot as $prd_hot_arr) {
					foreach ($check_prd_all as $check_prd_all_key => $check_prd_all_value) {
						if ($prd_hot_arr['product_id'] == $check_prd_all_value) {
							unset($check_prd_all[$check_prd_all_key]);
						}
					}	
				}
				
				foreach ($check_prd_all as $check_prd_all_arr) {
					$data_prd = array (
						'product_id' => $check_prd_all_arr,  			
					);		
										
					$this->mod_lib->insert('product_hot',$data_prd);
				}
			}	
			
			redirect('back_end/product_hot/index');
		}
		
		function delete($id) {
			
			if($this->mod_lib->deleteRecord('product_hot',$id)){
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			
			redirect('back_end/product_hot/index');
		}
}	
?>